package com.zyl.pagergallery_java;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zyl.pagergallery_java.adapter.PhotoAdapter;
import com.zyl.pagergallery_java.pojo.Pixabay;

import java.util.ArrayList;


public class PhotoFragment extends Fragment {
    ViewPager2 viewPager2;
    TextView textView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_photo, container, false);
        textView = inflate.findViewById(R.id.textView);
        viewPager2 = inflate.findViewById(R.id.viewPager2);
        return inflate;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        final ArrayList photo_list = getArguments().getParcelableArrayList("PHOTO_LIST");

        PhotoAdapter photoAdapter = new PhotoAdapter();

        viewPager2.setAdapter(photoAdapter);
        photoAdapter.submitList(photo_list);
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                textView.setText(position + 1 + " / " + photo_list.size());
            }
        });

        viewPager2.setCurrentItem(getArguments().getInt("PHOTO_POSITION"), false);


    }
}